package com.boot2.core.scheduler;

import com.boot2.core.dao.i.SchedulerTaskDao;
import com.boot2.core.model.SchedulerTask;
import com.boot2.core.utils.SpringUtil;
import lombok.extern.apachecommons.CommonsLog;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;

/**
 * @author zhangweilin
 * @Description: xxxxx
 * @date 2020/9/29
 */
@CommonsLog
public class JobExecutionContextHelper {

    /**
     * 更新保存与触发时间有关的信息
     *
     * @param jobExecutionContext
     */
    public static void updateSchedulerTaskFireTime(JobExecutionContext jobExecutionContext) {
        JobDataMap mergedJobDataMap = jobExecutionContext.getMergedJobDataMap();
        String code = (String) mergedJobDataMap.get(SchedulerJobDataMapConsant.code);
        String bussinessId = (String) mergedJobDataMap.get(SchedulerJobDataMapConsant.bussinessId);
        System.out.println("code = " + code);
        System.out.println("bussinessId = " + bussinessId);

        try {
            SchedulerTask schedulerTask = get(code, bussinessId);
            if (null != schedulerTask) {
                schedulerTask.setPreviousFireTime(jobExecutionContext.getPreviousFireTime());
                schedulerTask.setNextFireTime(jobExecutionContext.getNextFireTime());
                schedulerTask.setScheduledFireTime(jobExecutionContext.getScheduledFireTime());
                schedulerTask.setCurrentFireTime(jobExecutionContext.getFireTime());
                saveAndFlush(schedulerTask);
            }
        } catch (Throwable e) {
            e.printStackTrace();
            System.err.println("定时任务触发后，相关记录处理失败, " + e.getMessage());
        }
    }

    /**
     * 获取定时任务
     *
     * @param code
     * @param bussinessId
     * @return
     */
    public static SchedulerTask get(String code, String bussinessId) {
        SchedulerTaskDao schedulerTaskDao = SpringUtil.getBean(SchedulerTaskDao.class);
        if (null != schedulerTaskDao) {
            SchedulerTask schedulerTask = schedulerTaskDao.findByCodeAndBussinessId(code, bussinessId);
            if (schedulerTask == null) {
//                System.err.println("没有对应的schedulerTask,code: " + code + ", bussinessId: " + bussinessId);
                log.error("没有对应的schedulerTask,code: " + code + ", bussinessId: " + bussinessId);
                return null;
            }
            return schedulerTask;
        }
        return null;
    }

    /**
     * 获取
     * @param jobExecutionContext
     * @return
     */
    public static SchedulerTask get(JobExecutionContext jobExecutionContext) {
        JobDataMap mergedJobDataMap = jobExecutionContext.getMergedJobDataMap();
        String code = (String) mergedJobDataMap.get(SchedulerJobDataMapConsant.code);
        String bussinessId = (String) mergedJobDataMap.get(SchedulerJobDataMapConsant.bussinessId);
        return get(code, bussinessId);
    }

    public static String getCode(JobExecutionContext jobExecutionContext) {
        JobDataMap mergedJobDataMap = jobExecutionContext.getMergedJobDataMap();
        String code = (String) mergedJobDataMap.get(SchedulerJobDataMapConsant.code);
        return code;
    }

    public static String getBussinessId(JobExecutionContext jobExecutionContext) {
        JobDataMap mergedJobDataMap = jobExecutionContext.getMergedJobDataMap();
        String bussinessId = (String) mergedJobDataMap.get(SchedulerJobDataMapConsant.bussinessId);
        return bussinessId;
    }

    /**
     * 保存或更新
     *
     * @param schedulerTask
     * @return
     */
    public static SchedulerTask saveAndFlush(SchedulerTask schedulerTask) {
        SchedulerTaskDao schedulerTaskDao = SpringUtil.getBean(SchedulerTaskDao.class);
        if (null != schedulerTaskDao) {
            //更新被触发次数
            SchedulerTask schedulerTask1 = schedulerTaskDao.saveAndFlush(schedulerTask);
            schedulerTaskDao.updateFireCount(schedulerTask.getId());
            return schedulerTask1;
        }
        return null;
    }
}
